<?php

namespace App\Models\SystemManagement;

use App\Models\CommonModel;
use App\Models\Constant\Fields;
use App\Models\Error;

class Department extends CommonModel
{
    protected $table = 'departments';
    public $primaryKey = 'id';

    protected $hidden = ['deleted_at','created_at','updated_at'];

    /**
     * @param $data 表单提交数据
     * @return string 返回响应结果
     * 添加部门数据
     */
    public static function addData($data){
        $param = new self();
        $mark = $param->orderBy(Fields::MARK,'desc')->value(Fields::MARK);
        if(is_null($mark)){
            $data['mark'] = 1;
        }else{
            $data['mark'] = seriesNum($mark);
        }
        $data['creator'] = session('login.name')?session('login.name'):'admin';
        $data['full_name'] = $data['name'];
        $re = $param->create($data);
        return $re;
    }

    /**
     * @return mixed
     * 获取部门 分页列表
     */
    public static function getPageData($per){
        $query = self::with('organization')->where('disabled_state','<>',1)->orderBy(Fields::ID)->paginate($per);
        return $query;
    }

    /**
     * @return mixed
     * 获取部门列表
     */
    public static function getAll($request){
        return self::orderBy(Fields::ID)
                ->with('organization')
                ->where(function($query) use ($request){
                    $organization_id = $request->input('organization_id','');
                    if(!empty($organization_id)) {
                        $query->where('organization_id',$organization_id);
                    }
                })
                ->where('disabled_state','<>',1)
                ->get(['id','name','parent_id','organization_id']);
    }

    /**
     * @param $id
     * @return mixed
     * 获取单条部门数据
     */
    public static function getOne($id){
        $query = self::find($id);
        return $query;
    }

    /**
     * @param $con
     * @return mixed
     * 通过条件查找
     */
    public static function getByCondition($con){
        $query = self::getTableDataByCondition($con,new self());
        return $query;
    }

    /**
     * @param $arr
     * @param $id
     * @return mixed
     * 修改某条数据
     */
    public static function updateData($arr,$id){
        if(isset($arr['disabled_state'])){
            $arr['disable'] = session('login.name')?session('login.name'):'admin';
        }
        $query = self::updateTableData($arr,$id,new self());
        return $query;
    }

    /**
     * @param $ids
     * @return Error
     * @throws \Exception
     * 删除
     */
    static function del($ids){
        $query = self::batchDelTableData($ids,new self());
        return $query;
    }

    /**
     * @param $ids
     * @return Error
     * 恢复
     */
    static function recover($ids){
        $query = self::batchResetTableData($ids,new self());
        return $query;
    }

    //组织部门关系
    public function organization()
    {
        return $this->belongsTo(self::$SYSTEM.'\Organization',Fields::ORGAN_ID,Fields::ID)->select('id','name');
    }

    /**
     * 部门分类
     * @return mixed
     */
    static function getCategory(){
        $array = parent::getTableDataDataToArr(new self());
        return $array;
    }

    function users(){
        return $this->hasMany(Employee::class,'department_id');
    }


}
